package com.xq.shop.controller;

import com.xq.shop.dto.EditNoticeDto;
import com.xq.shop.dto.IntegerDto;
import com.xq.shop.dto.QueryDto;
import com.xq.shop.service.INoticeService;
import com.xq.shop.service.IUserService;
import com.xq.shop.vo.NoticeVo;
import com.xq.shop.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import utils.BasePage;
import utils.BaseResult;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 公告表 前端控制器
 * </p>
 *
 * @author gaoxiaojin
 * @since 2024-07-22
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {

    private final INoticeService noticeService;
    private final IUserService userService;

    @Autowired
    public NoticeController(INoticeService noticeService, IUserService userService) {
        this.noticeService = noticeService;
        this.userService = userService;
    }

    @RequestMapping("/page")
    public BaseResult<BasePage<NoticeVo>> page(@RequestBody QueryDto dto, HttpServletRequest request) {
        userService.getInfo(request);
        BaseResult<BasePage<NoticeVo>> result = new BaseResult<>();
        BasePage<NoticeVo> page = noticeService.pageQuery(dto);
        result.initializeSuccess(page);
        return result;
    }

    @RequestMapping("/query")
    public BaseResult<List<NoticeVo>> query(@RequestBody QueryDto dto, HttpServletRequest request) {
        userService.getInfo(request);
        BaseResult<List<NoticeVo>> result = new BaseResult<>();
        List<NoticeVo> page = noticeService.queryNotice(dto);
        result.initializeSuccess(page);
        return result;
    }

    @RequestMapping("/edit")
    public BaseResult<Void> query(@RequestBody @Validated EditNoticeDto dto, HttpServletRequest request) {
        UserVo vo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        noticeService.editNotice(dto, vo.getId());
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/remove")
    public BaseResult<Void> remove(@RequestBody @Validated IntegerDto dto, HttpServletRequest request) {
        UserVo vo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        noticeService.removeNotice(dto, vo.getId());
        result.initializeSuccess();
        return result;
    }


}
